﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t13DelegateAndEvent
{
    class Bank
    {
        //定义事件参数：观察者关心的内容
        public class UserEventArgs : EventArgs
        {
            public string Email;
            public string Mobile;
            public string Amount;

            public UserEventArgs(string email, string mobile, string amount)
            {
                Email = email;
                Mobile = mobile;
                Amount = amount;
            }
        }
        //事件发布者：银行
        public class Bank
        {
            //声明委托：银行交易
            public delegate void ProcessTranEventHandler(Object sender, UserEventArgs e);
            //声明事件
            public event ProcessTranEventHandler ProcessTran;

            protected virtual void OnProcessTran(UserEventArgs e)
            {
                if (ProcessTran != null)
                {
                    ProcessTran(this, e);
                }
            }
            //触发事件
            public void Process(UserEventArgs e)
            {
                OnProcessTran(e);
            }
        }
        //事件观察者：电子邮件
        public class Email
        {
            public static void SendMail(Object sender, UserEventArgs e)
            {
                Console.WriteLine($"向用户邮箱{e.Email}发送邮件：您在{System.DateTime.Now}取出金额为{e.Amount}元"
    );
            }
        }
        //事件观察者：短信
        public class MobileSMS
        {
            public static void SendNotification(Object sebder, UserEventArgs e)
            {
                Console.WriteLine($"向用户邮箱{ e.Email}发送邮件：您在{ System.DateTime.Now}取款金额为{e.Amount}元");
            }
        }
        //订阅系统：松耦合
        public class SubscribeSystem
        {
            public SubscribeSystem() { }
            public SubscribeSystem(Bank bank, UserEventArgs e)
            {
                bank.ProcessTran += Email.SendMail;
                bank.ProcessTran += MobileSMS.SendNotification;
                bank.Process(e);
            }
        }
    }




}
}
